package com.antifly.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 设置FastJson日期格式类型转换
 *
 * @author LuoJiaHui
 * @date 2023/4/25 9:01
 * @description
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * Extend or modify the list of converters after it has been, either
     * {@link #configureMessageConverters(List) configured} or initialized with
     * a default list.
     * <p>Note that the order of converter registration is important. Especially
     * in cases where clients accept {@link MediaType#ALL}
     * the converters configured earlier will be preferred.
     *
     * @param converters the list of configured converters to be extended
     * @since 4.1.3
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(0, fastJsonHttpMessageConverter);
    }
}
